<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends CI_Controller {

	public function __construct(){
		parent::__construct();
		$this->load->model('login_model','l_db');
	}

	/**
	 * Index Page for this controller.
	 */
	public function index()
	{
		if($this->session->userdata('username')){			
			$header['menu'] 		= $this->l_db->generate_menu();
			$data['tbl_guru'] 		= $this->l_db->get_count('tbl_guru')->num_rows();
			$data['tbl_orang_tua'] 	= $this->l_db->get_count('tbl_orang_tua')->num_rows();
			$data['tbl_sekolah'] 	= $this->l_db->get_count('tbl_sekolah')->num_rows();
			$data['tbl_siswa'] 		= $this->l_db->get_count('tbl_siswa')->num_rows();
			$data['level'] 			= $this->session->userdata('level');
			$data['username'] 		= $this->session->userdata('username');
			$this->load->view('header_view',$header);
			$this->load->view('home_view',$data);
			$this->load->view('footer_view');
		}else{
			$header['no_visible_elements'] = true;
			$this->load->view('header_view',$header);
			$this->load->view('login_view');
			$this->load->view('footer_view');
		}
	}

	public function do_login(){
		if($_POST){
			$this->load->model('login_model','get_db');
			$param = array(
				'username' => $_POST['username'],
				'password' => md5($_POST['password'])
				);
			$check = $this->get_db->do_login($param);			
			if($check->num_rows()>0){
				$this->session->set_userdata($check->row_array());
				redirect();
			}else{
				$this->session->set_flashdata( 'message', array( 'title' => 'Error', 'content' => 'User not found or password invalid !', 'type' => 'message error close' )); 
				redirect();
			}
		}else{
			die('Unable to access!!');
		}

	}

	public function do_logout(){
		$this->session->sess_destroy();
		redirect();
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */